use std::io::Read;

/* 转String 类型 */
pub fn vec_string(list: &mut Vec<u8>) -> String {
    let str = match std::str::from_utf8_mut(list) {
        Ok(s) => { s }
        Err(_e) => { "" }
    };
    return str.to_string();
}

/**
 * byte 数组 转成String类型
 */
pub fn byte_string(bytes: &[u8]) -> String {
    let str = match std::str::from_utf8(bytes) {
        Ok(s) => { s }
        Err(_e) => { "" }
    };
    return str.to_string();
}

/* 测试utf-8的编码逻辑 */
pub(crate) fn test_string_jx(content: &str) {
    let bytes = content.as_bytes();
    let length = bytes.len();

    let mut i: usize = 0;

    while i < length {
        let b01 = bytes[i];
        if b01 <= 0x7F { //取一位
            println!("显示为： {}", b01);
            i = i + 1;
        } else if (b01 & 0xE0) == 0xC0 {
            //取两位位
            if (i + 2 <= length) {
                let b2 = &bytes[i..i+2];
                println!("显示为： {:?}", b2);
            }
            i = i + 2;
        }else if (b01 & 0xF0) == 0xE0 {
            //取三位位
            if (i + 3 <= length) {
                let b2 = &bytes[i..i+3];
                println!("显示为： {:?}", b2);
            }
            i = i + 3;
        }else if (b01 & 0xF8) == 0xF0 {
            //取4位位
            if (i + 4 <= length) {
                let b2 = &bytes[i..i+4];
                println!("显示为： {:?}", b2);
            }
            i = i + 4;
        }
    }
}


